<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  
</body>
<script>

  function fun(){
    let a = 10;
    a++;
    console.log(a);
  }
  fun();
  fun();
  fun();
  fun();
  fun();
  fun();


  // ============


  let fn = (function(){
    let a = 10;
    return function(){
      a++;
      console.log(a);
    }
  })();
  
  fn();
  fn();
  fn();
  fn();
  fn();
  fn();
  fn();
  fn();

  // console.log(a);


  // ============

  // 沙箱模式
  let foo = (function(){
    let a = "hello123";
    let b = "world";

    let getA = function(){
      return a;
    }
    let setA = function(msg){
      a = msg;
    }
    let getB = function(){
      return b;
    }
    let setB = function(msg){
      b = msg
    }

    return {
      getA, setA, getB, setB
    };

  })();

  console.log( foo.getA() );
  foo.setA( "hahaha" );
  console.log( foo.getA() );


  console.log( foo.getB() );
  foo.setB("365");
  console.log( foo.getB() );
  
</script>
</html>